package org.vgou.action.manage;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.vgou.model.Business;
import org.vgou.model.Category;
import org.vgou.model.Location;
import org.vgou.page.Page;
import org.vgou.page.PageUntils;
import org.vgou.service.IBusinessService;
import org.vgou.service.ICategoryService;
import org.vgou.service.ILocationService;
import org.vgou.util.StrutsUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * Copyright (c) 2011, vgou <br>
 * All rights reserved.<br>
 * 
 * @since 2011-11-30
 * @author <a href="mailto:jason.hxx@gmail.com">jason</a>
 * @version Id: BusinessManageAction.java
 * @comment 商家类型操作类
 * 
 */
@Component("businessManageAction")
@Scope("prototype")
public class BusinessManageAction  extends ActionSupport implements ModelDriven<Business>{

	private static final long serialVersionUID = -7827404172263303410L;
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	private int page;
	private long businessPK;
	private long categoryPK;
	private long locationPK;
	private int searchStatus;
	private int result;
	private String searchName;
	private Business business = new Business();
	private List<Business> businessList;
	private List<Category> categoryList;
	private List<Location> provinceList;
	@Resource
	private IBusinessService businessService;
	@Resource
	private ICategoryService categoryService;
	@Resource
	private ILocationService locationService;
	
	/**
	 * 初始化商家添加或修改表单
	 * @return
	 * @throws Exception
	 */
	public String initForm() throws Exception{
		try {
			if(businessPK > 0){
					business = businessService.getById(businessPK);
			}
			categoryList = categoryService.getAll("", "", true, 0, Integer.MAX_VALUE);
			provinceList = locationService.getAll("", 0, "", true, 0, Integer.MAX_VALUE);
		} catch (Exception e) {
			logger.error("初始化商家表单失败:" + e.getMessage());
		}
		return "initForm";
	}
	
	/**
	 * 添加或修改商家信息
	 * @return
	 * @throws Exception
	 */
	public String addOrUpdate() throws Exception{
		try {
			if(business.getId() <= 0){
				business.setCategory(categoryService.getById(categoryPK));
				business.setLocation(locationService.getById(locationPK));
				long id = businessService.create(business);
				if(id > 0)
					result = 1;
				else
					result = -1;
			}else{
				result = businessService.update(business);
			}
		} catch (Exception e) {
			logger.error("更新商家信息失败:" + e.getMessage());
		}
		return "result";
	}
	
	/**
	 * 取得商家列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception{
		try {
			int perPage = 8;//每页的显示记录数
			int total = businessService.getAllCount(searchName, searchStatus);
			String params = null;
			if(searchName != null || searchStatus != 0){
				params = "businessName=" + searchName + "&status=" + searchStatus;
			}
			Page pageModel = PageUntils.getPageBar(StrutsUtils.getRequest(), page, perPage , total , params, "page");
			businessList = businessService.getAll(searchName, searchStatus, null, false, pageModel.getStartRs(), pageModel.getPerPage());
		} catch (Exception e) {
			logger.error("取得商家列表失败:" + e.getMessage());
		}
		return "list";
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public long getBusinessPK() {
		return businessPK;
	}

	public void setBusinessPK(long businessPK) {
		this.businessPK = businessPK;
	}

	public Business getBusiness() {
		return business;
	}

	public void setBusiness(Business business) {
		this.business = business;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}
	
	public List<Business> getBusinessList() {
		return businessList;
	}

	public void setBusinessList(List<Business> businessList) {
		this.businessList = businessList;
	}

	@Override
	public Business getModel() {
		return business;
	}

	public int getSearchStatus() {
		return searchStatus;
	}

	public void setSearchStatus(int searchStatus) {
		this.searchStatus = searchStatus;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public List<Location> getProvinceList() {
		return provinceList;
	}

	public void setProvinceList(List<Location> provinceList) {
		this.provinceList = provinceList;
	}

	public long getCategoryPK() {
		return categoryPK;
	}

	public void setCategoryPK(long categoryPK) {
		this.categoryPK = categoryPK;
	}

	public long getLocationPK() {
		return locationPK;
	}

	public void setLocationPK(long locationPK) {
		this.locationPK = locationPK;
	}

}
